You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.6 KiB
51 lines
1.6 KiB
import { defineWrappedResponseHandler } from "#server/utils/handler";
|
|
import { R } from "#server/utils/response";
|
|
import { resolveAgentIdentity } from "#server/service/agent/identity";
|
|
import { getSessionByIdAndUser } from "#server/service/agent/session";
|
|
import { getAgentBySlug } from "#server/service/agent/agent";
|
|
import { getStreamBuffer, markBufferDone } from "#server/service/agent/stream-buffer";
|
|
import { abortSessionStream } from "#server/service/agent/abort-manager";
|
|
|
|
export default defineWrappedResponseHandler(async (event) => {
|
|
const agentSlug = getRouterParam(event, "agentSlug");
|
|
if (!agentSlug) {
|
|
return R.error("参数无效", null);
|
|
}
|
|
|
|
const agent = await getAgentBySlug(agentSlug);
|
|
if (!agent) {
|
|
return R.error("agent 不存在", null);
|
|
}
|
|
|
|
const identity = await resolveAgentIdentity(event);
|
|
if (!identity.userId && !identity.tempToken) {
|
|
return R.error("请先登录或创建临时会话", null);
|
|
}
|
|
|
|
const body = await readBody(event);
|
|
const { sessionId } = body as { sessionId?: string };
|
|
|
|
if (!sessionId) {
|
|
return R.error("缺少 sessionId 参数", null);
|
|
}
|
|
|
|
const session = await getSessionByIdAndUser(sessionId, identity.userId, identity.tempToken);
|
|
if (!session) {
|
|
return R.error("会话不存在", null);
|
|
}
|
|
|
|
if (session.agentId !== agent.id) {
|
|
return R.error("会话不属于该 agent", null);
|
|
}
|
|
|
|
const aborted = abortSessionStream(sessionId);
|
|
|
|
if (!aborted) {
|
|
const buf = getStreamBuffer(sessionId);
|
|
if (buf && !buf.done) {
|
|
markBufferDone(sessionId);
|
|
}
|
|
}
|
|
|
|
return R.success({ aborted });
|
|
});
|
|
|